package com.example.mobile2030606155byl0625homework;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private ImageView imageView2;
    private TextView tvCityName;
    private TextView textView5;
    private TextView tvCondition;
    private TextView tvTemp;
    private TextView tvWindDir;
    private TextView tvWindLevel;
    private TextView tvValue;
    private Button btnmainCondition;
    OkHttpClient okHttpClient = new OkHttpClient();
    private TextView tvHumidity;
    private TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnmainCondition.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        RequestBody requestBody = new FormBody.Builder()
                                .add("cityId", "920")
                                .add("token", MojiDataManager.CONDITIONTOKEN)
                                .build();
                        Request request = new Request.Builder()
                                .addHeader("Authorization", "APPCODE " + MojiDataManager.APPCODE)
                                .url(MojiDataManager.CONDITIONURL)
                                .post(requestBody)
                                .build();
                        Call call = okHttpClient.newCall(request);
                        call.enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {

                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                String result = response.body().string();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Gson gson=new Gson();
                                        Condition condition=gson.fromJson(result,Condition.class);
                                        tvCityName.setText(condition.getData().getCity().getName());
                                        tvCondition.setText(condition.getData().getCondition().getCondition());
                                        tvTemp.setText(condition.getData().getCondition().getTemp()+"°");
                                        tvWindDir.setText("风向："+condition.getData().getCondition().getWindDir());
                                        tvWindLevel.setText(condition.getData().getCondition().getWindLevel()+"级");
                                        tvValue.setText("空气质量：良");
                                        tvHumidity.setText("湿度："+condition.getData().getCondition().getHumidity()+"°");
                                        textView2.setText(result);
                                        Log.i("TAG", "run: " + result);
                                    }
                                });

                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView2 = (ImageView) findViewById(R.id.imageView2);
        tvCityName = (TextView) findViewById(R.id.tvCityName);
        textView5 = (TextView) findViewById(R.id.textView5);
        tvCondition = (TextView) findViewById(R.id.tvCondition);
        tvTemp = (TextView) findViewById(R.id.tvTemp);
        tvWindDir = (TextView) findViewById(R.id.tvWindDir);
        tvWindLevel = (TextView) findViewById(R.id.tvWindLevel);
        tvValue = (TextView) findViewById(R.id.tvValue);
        btnmainCondition = (Button) findViewById(R.id.btnmainCondition);
        tvHumidity = (TextView) findViewById(R.id.tvHumidity);
        textView2 = (TextView) findViewById(R.id.textView2);
    }
}